package com.vainycos.lotterysearch.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.vainycos.lotterysearch.enums.LotteryEnum;
import com.vainycos.lotterysearch.model.LuckyLottery;
import com.vainycos.lotterysearch.model.worldcup.Worldcup;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: Vainycos
 * @description 免费接口api
 * @date: 2022/11/16 10:17
 */
@Slf4j
public class FreeApiUtil {

    /**
     * free-api接口信息，详见https://www.free-api.com/
     */
    private static String appSecret = "xxx";
    private static String appId = "xxx";

    /**
     * 每日一句
     * https://www.free-api.com/doc/583
     * {
     * 	"code": 1,
     * 	"msg": "数据返回成功！",
     * 	"data": [{
     * 		"content": "承君此诺，必守一生。",
     * 		"author": "锦重"
     *        }]
     * }
     * @return
     */
    public static String dailyWord(){
        String resule = HttpUtil.get("https://www.mxnzp.com/api/daily_word/recommend?app_secret=" + appSecret + "&app_id=" + appId);
        JSONObject jsonObject = JSONObject.parseObject(resule);
        JSONObject data = (JSONObject) jsonObject.getJSONArray("data").get(0);
        return data.getString("content");
    }

    /**
     * 彩票开奖查询
     * {
     * 	"code": 1,
     * 	"msg": "数据返回成功！",
     * 	"data": {
     * 		"openCode": "03,06,10,11,27,33+03",
     * 		"code": "ssq",
     * 		"expect": "2019135",
     * 		"name": "双色球",
     * 		"time": "2019-11-24 21:18:20"
     *        }
     * }
     * @param periodId 期数
     * @param lotteryEnum 彩票类型 彩票种类标识，目前提供七种彩种， ssq：双色球，qlc：七乐彩，fc3d：福彩3D，cjdlt：超级大乐透，qxc：七星彩，pl3：排列3，pl5：排列5
     * @return
     */
    public static LuckyLottery luckyLottery(int periodId, LotteryEnum lotteryEnum){
        String resule = HttpUtil.get("https://www.mxnzp.com/api/lottery/common/aim_lottery?app_secret=" + appSecret + "&app_id=" + appId + "&code=" + lotteryEnum.type + "&expect=" + periodId);
        JSONObject jsonObject = JSONObject.parseObject(resule);
        System.out.println(JSONObject.toJSONString(jsonObject));
        return JSONObject.parseObject(jsonObject.getString("data"), LuckyLottery.class);

    }

    /**
     * 当日天气接口
     * @return
     */
    public static String realTimeweather(){
        String s = HttpUtil.get("https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm");
        JSONObject jsonObject = JSONObject.parseObject(s);
        // 数据最近更新时间
        String update_time = jsonObject.getString("update_time");
        JSONObject todayWeather = jsonObject.getJSONArray("data").getJSONObject(0);
        StringBuffer todayDesc = new StringBuffer();
//        todayDesc.append("> 早安打工人" + " \n");
        todayDesc.append("# 今日天气" + " \n");
        todayDesc.append(todayWeather.getString("date") + " " + todayWeather.getString("week") + " \n");
        todayDesc.append("数据更新时间：" + update_time + " \n");
        todayDesc.append("日出时间：" + todayWeather.getString("sunrise") + " 日落时间" + todayWeather.getString("sunset") + " \n");
        todayDesc.append("月相：" + todayWeather.getString("moonPhrase") + " \n");
        todayDesc.append("空气质量：" + todayWeather.getString("air_level") + " \n");
        todayDesc.append(todayWeather.getString("narrative") + " \n");
        log.info("天气数据获取成功");
        return todayDesc.toString();
    }

    /**
     * 下午茶接口
     */
    public static String drink(){
        List<String> drinks = new ArrayList<>();
        drinks.add("拿铁");
        drinks.add("摩卡");
        drinks.add("蓝山咖啡");
        drinks.add("焦糖玛奇朵");
        drinks.add("卡布奇诺");
        drinks.add("意式浓缩");

        // 奶茶
        drinks.add("西柚茉莉");
        drinks.add("草莓脏脏奶");
        drinks.add("黑加仑脏脏奶");
        drinks.add("草莓冰沙");
        drinks.add("芒果冰沙");
        drinks.add("满杯红柚");
        drinks.add("抹茶奶昔");
        drinks.add("冻顶乌龙奶盖");
        int round = (int) Math.round(Math.random() * 13);
        String image = "";
        if (round > 5){
            image = "![good-book-and-a-stiff-drink](https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fmarketplace.canva.cn%2F6HvwA%2FMAE1RT6HvwA%2F1%2Ftl-1%2Fcanva-takeaway-drinks-MAE1RT6HvwA.png&refer=http%3A%2F%2Fmarketplace.canva.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1671931685&t=66f1068bf3dae6529ecded2ea06eeacb)";
        }else {
            image = "![coffee](https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbkimg.cdn.bcebos.com%2Fpic%2Fb21bb051f8198618367a531020a239738bd4b31c485d&refer=http%3A%2F%2Fbkimg.cdn.bcebos.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1671347195&t=d53d8cf43414bd45c025b04e84deff28)";
        }
        String drinking = "下午茶时间，饮茶先啦！\n今天适合来一杯" + drinks.get(round) + " \n" +
                image;
        return moyuya() + drinking;
    }

    /**
     * 获取对应日期的赛事信息
     * @param date 2022-11-20
     * @return
     */
    public static Worldcup getWorldcupByDate(String date){
        String s = HttpUtil.get("https://tiyu.baidu.com/api/match/%E4%B8%96%E7%95%8C%E6%9D%AF/live/date/2022-" + date + "/direction/after");
        return JSONObject.parseObject(s, Worldcup.class);
    }

    /**
     * 获取本届所有赛事的对应日期的赛事信息
     * @return
     */
    public static List<Worldcup> getAllWorldcupInfo(){
        List<Worldcup> worldcups = new ArrayList<>();
        List<String> times = Arrays.asList("11-20", "11-21", "11-22", "11-23", "11-24", "11-25", "11-26", "11-27",
                "11-28", "11-29", "11-30", "12-01", "12-02", "12-03", "12-04", "12-05",
                "12-06", "12-08", "12-09", "12-10", "12-13", "12-14", "12-16", "12-17");
        times.forEach(date->{
            String s = HttpUtil.get("https://tiyu.baidu.com/api/match/%E4%B8%96%E7%95%8C%E6%9D%AF/live/date/2022-" + date + "/direction/after");
            Worldcup worldcup = JSONObject.parseObject(s, Worldcup.class);
            worldcups.add(worldcup);
        });
        return worldcups;
    }

    /**
     * 摸鱼鸭提醒
     * @return
     */
    public static String moyuya(){
        StringBuffer stringBuffer = new StringBuffer();
        String s = HttpUtil.get("https://api.moyuduck.com/holiday");
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONObject data = jsonObject.getJSONObject("data");
        stringBuffer.append("![moyuya](https://moyuduck.com/static/duck2.gif)" + " \n");
        stringBuffer.append("【摸鱼鸭提醒您】 今天是 " + data.getString("time") + " " + data.getString("week"));
        stringBuffer.append(" \n");
        stringBuffer.append("你好，摸鱼人，工作再累，一定不要忘记摸鱼哦 ！" + " \n");
        stringBuffer.append("有事没事起身去茶水间去廊道去天台走走，别老在工位上坐着。" + " \n");
        stringBuffer.append("多喝点水，钱是老板的，但命是自己的 !" + " \n");
        stringBuffer.append(" \n");
        stringBuffer.append("\uD83D\uDC1F " + data.getString("weekDay") + " \n");
        data.getJSONArray("list").forEach(e->{
            JSONObject jsonObject1 = (JSONObject) e;
            stringBuffer.append("\uD83D\uDC1F 距离" + jsonObject1.getString("name") + jsonObject1.getString("date") + " 放假还有" + jsonObject1.getString("day") + "天 \n");

        });
        stringBuffer.append("【友情提示】三甲医院 ICU 躺一天平均费用大概一万块。" + " \n \n");
        stringBuffer.append("你晚一天进 ICU，就等于为你的家庭多赚一万块。少上班，多摸鱼。" + " \n \n");
        stringBuffer.append("上班是帮老板赚钱，摸鱼是赚老板的钱！最后，祝愿天下所有摸鱼人，都能愉快的渡过每一天 !" + " \n \n");
        return stringBuffer.toString();
    }

    /**
     * 热点新闻
     */
    public static JSONObject hotNews(){
        String url = "https://api.moyuduck.com/hot/all";
        String s = HttpUtil.get(url);
        return JSONObject.parseObject(s);
    }

    /**
     * 幸运号码推荐
     * @param args
     * @throws InterruptedException
     */

}
